﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BLL
{
    public class ChuSuDungBLL
    {
        LandEntities data = new LandEntities();
        public ChuSuDungBLL()
        { }

        public List<CHUSUDUNG> TimDanhSachChuSuDung(string maCSH)
        {
            var chusudung = from csd in data.CHUSUDUNGs
                            where csd.MACSD == maCSH
                            select csd;
            return chusudung.ToList<CHUSUDUNG>();
        }

        public string TimVaLayMaCSD(string tenCSD, string soCMND)
        {
            var chusudung = (from cs in data.CHUSUDUNGs
                                   where cs.TENCSD.Contains(tenCSD) || cs.SO_CMND_QDTL_GPKD == soCMND
                                   select cs).First();
            return chusudung.MACSD;
        }

        public List<TIMCHUSUDUNG_Result> TimVaLayChuSuDung(string tenCSD, string soCMND)
        {
            var chusudung = from csd in data.TIMCHUSUDUNG(tenCSD, soCMND)
                            select csd;
            return chusudung.ToList<TIMCHUSUDUNG_Result>();
        }
        //public List<CHUSUDUNG> TimVaLayChuSuDung(string tenCSD, string soCMND)
        //{
        //    var chusudung = from cs in data.CHUSUDUNGs
        //                     where cs.TENCSD.Contains(tenCSD) || cs.SO_CMND_QDTL_GPKD == soCMND
        //                     select cs;
        //    return chusudung.ToList<CHUSUDUNG>();
        //}

        //public void ThemChuSuDungVoGiayChungNhan(string maGCN, string maCSD)
        //{
        //    GCN_SDD gcn = data.GCN_SDD.Single(g => g.MAGCN == maGCN);
        //    gcn.CHUSUDUNGs.Add(new CHUSUDUNG() { MACSD = maCSD });
        //    data.GCN_SDD.Attach(gcn);
        //    foreach (var giaychungnhan in gcn.CHUSUDUNGs)
        //    {
        //        data.ObjectStateManager.ChangeObjectState(giaychungnhan, EntityState.Unchanged);

        //    }
        //    data.SaveChanges();
        //}
        public void ThemChuSuDung(string maCSD, string tenCSD, string cMND, DateTime ngayCap,
            DateTime namSinh, string soNha, string tenDuong, string thon,string xaPhuong, string quocTich, string danToc, 
            string doiTuong)
        {
            CHUSUDUNG chuSuDung = new CHUSUDUNG();
            chuSuDung.MACSD = maCSD;
            chuSuDung.TENCSD = tenCSD;
            chuSuDung.SO_CMND_QDTL_GPKD = cMND;
            chuSuDung.NGAYCAP_CMND_QDTL_GPKD = ngayCap;
            chuSuDung.NAMSINH_THANHLAP = namSinh;
            chuSuDung.SONHA = soNha;
            chuSuDung.TENDUONG = tenDuong;
            chuSuDung.THON = thon;
            chuSuDung.MAXP = xaPhuong;
            chuSuDung.MADANTOC = danToc;
            chuSuDung.MADT = doiTuong;

            chuSuDung.QUOCTICHes.Add(new QUOCTICH() { MAQT = quocTich });

            data.CHUSUDUNGs.AddObject(chuSuDung);

            foreach (var quoctich in chuSuDung.QUOCTICHes)
            {
                data.ObjectStateManager.ChangeObjectState(quoctich, EntityState.Unchanged);
            }

            data.SaveChanges();
        }

        public CHUSUDUNG TimChuSuDung(string ten, string cmnd)
        {
            var chusudung = data.CHUSUDUNGs.Single(csd => csd.MACSD.Contains(ten) || csd.SO_CMND_QDTL_GPKD == cmnd);
            return chusudung;
        }
    }
}
